﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using OpenLab.Model.ViewModel;
namespace OpenLab.DAL.ViewModel
{
    public class ListeClientDAL
    {
        public ListeClient ToModel(SqlDataReader reader)
        {
            ListeClient client = new ListeClient();

            client.nomComplet = (string)ToModelValue(reader, "nomComplet");
            client.ageday = (int)ToModelValue(reader, "ageday");
            client.id = (int)ToModelValue(reader, "id");
            client.nom = (string)ToModelValue(reader, "nom");
            client.prenom = (string)ToModelValue(reader, "prenom");
            client.datenaissance = (DateTime)ToModelValue(reader, "datenaissance");
            client.sexe = (int)ToModelValue(reader, "sexe");
            client.tel = (string)ToModelValue(reader, "tel");
            client.idorganisme = (int?)ToModelValue(reader, "idorganisme");
            client.coderecherche = (string)ToModelValue(reader, "coderecherche");
            return client;
        }

        public IEnumerable<ListeClient> GetAll()
        {
            string sql = "SELECT * FROM ListeClient";
            using (SqlDataReader reader = SqlHelper.ExecuteDataReader(sql))
            {
                return ToModels(reader);
            }
        }

        protected IEnumerable<ListeClient> ToModels(SqlDataReader reader)
        {
            var list = new List<ListeClient>();
            while (reader.Read())
            {
                list.Add(ToModel(reader));
            }
            return list;
        }

        protected object ToDBValue(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }
            else
            {
                return value;
            }
        }

        protected object ToModelValue(SqlDataReader reader, string columnName)
        {
            if (reader.IsDBNull(reader.GetOrdinal(columnName)))
            {
                return null;
            }
            else
            {
                return reader[columnName];
            }
        }
    }
}
